package generator.src.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

/**
 * 数据库连接工具类
 * @author 黄为涛
 *
 */
public class JDBCUtil {

	private static String sqlDriver;
	private static String sqlUrl;
	private static String sqlUser;
	private static String sqlPassword;
	
	/**
	 * 初始化参数
	 */
	{
		try {
			sqlDriver=TaoUtil.getProperty("sqlDriver");
			sqlUrl=TaoUtil.getProperty("sqlUrl");
			sqlUser=TaoUtil.getProperty("sqlUser");
			sqlPassword=TaoUtil.getProperty("sqlPassword");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
	/**
	 * 获取一个连接
	 * @return
	 */
	public static Connection getConnection(){
		Connection con=null;
		try {
			Class.forName(sqlDriver);
			con=DriverManager.getConnection(sqlUrl,sqlUser,sqlPassword);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 关闭一个连接
	 * @param rs
	 * @param pstmt
	 * @param con
	 */
	public static void close(ResultSet rs,PreparedStatement pstmt,Connection con){
		try {
			if(rs!=null)
				rs.close();
			if(pstmt!=null)
				pstmt.close();
			if(con!=null)
				con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
